// pages/address/user-address/user-address.js
var app = getApp()
Page({
    data: {
        address: [],
        radioindex: '',
        pro_id:0,
        num:0,
        cartId:0,
        def_type:0
    },
    onLoad: function (options) {
        var that = this;
        // 页面初始化 options为页面跳转所带来的参数
        var cartId = options.cartId;
        var def_type = options.def_type;
        app.request("UserAddressService", "queryUserAddress",
            {
                uid: app.d.userId,
            }, function (data) {
                var address = data.adds;
                if (address == '') {
                    var address = []
                }
                if (def_type == undefined) { def_type = 0; }
                that.setData({
                    address: address,
                    cartId: cartId,
                    def_type: def_type
                })
            });
    },
    renovate:function(){
      var that = this;
      app.request("UserAddressService", "queryUserAddress",
        {
          uid: app.d.userId,
        }, function (data) {
          var address = data.adds;
          if (address == '') {
            var address = []
          }
          that.setData({
            address: address,
          })
        });
    },
    setDefault: function(e) {
        var that = this;
        var addressList = e.currentTarget.dataset.address;
        var addrId = addressList.address_id;
        app.request("UserAddressService", "setDefault",
            {
                uid: app.d.userId,
                addrId: addrId
            }, function (data) {
                var def_type = that.data.def_type;
                //跳转
                var pages = getCurrentPages();
                var currPage = pages[pages.length - 1];
                var prevPage = pages[pages.length - 2];
                if (def_type == 1) {
                    var pages = getCurrentPages();
                    var currPage = pages[pages.length - 1];
                    var prevPage = pages[pages.length - 2];
                    prevPage.setData({
                        address: addressList,
                        addemt:0,
                    });
                    wx.navigateBack();
                }
                that.DataonLoad();
            });
    },
    editAddress: function (e) {
        var address = e.currentTarget.dataset.address;
        var cartId = e.currentTarget.dataset.cartid;
        wx.navigateTo({
            url: '../address?address=' + JSON.stringify(address) + '&cartId=' + cartId + '&editType=0',
        })
    },
    newAddress: function (e) {
      var cartId = e.currentTarget.dataset.cartid;
      wx.navigateTo({
        url: '../address?cartId=' + cartId,
      })
    },
    delAddress: function (e) {
        var that = this;
        var addrId = e.currentTarget.dataset.id;
        var name = e.currentTarget.dataset.name;
        var phone = e.currentTarget.dataset.phone;

        wx.showModal({
            title: '提示',
            content: '你确认移除吗',
            success: function(res) {
                res.confirm && app.request("UserAddressService", "delUserAddress",
                    {
                        uid: app.d.userId,
                        addrId: addrId
                    }, function (data) {
                        that.DataonLoad();
                    });
            }
        });

    },
    DataonLoad: function () {
        var that = this;
        // 页面初始化 options为页面跳转所带来的参数
        app.request("UserAddressService", "queryUserAddress",
            {
                uid: app.d.userId,
            }, function (data) {
                var address = data.adds;
                if (address == '') {
                    var address = []
                }

                that.setData({
                    address: address,
                })
            });
    },
})